package com.shiwaixiangcun.customer.pay

import android.app.Activity
import android.widget.Toast
import com.google.gson.reflect.TypeToken
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.shiwaixiangcun.customer.entity.ResponseEntity
import com.shiwaixiangcun.customer.utils.JsonUtil
import com.shiwaixiangcun.customer.utils.MD5Util

/**
 * 支付工具类
 *
 * @author Administrator
 */

object PayUtil {
    private val BUG_TAG = "PayUtil"

    /**
     * 调用支付宝进行支付
     *
     * @param payUrl      支付路径
     * @param orderNumber 订单号
     * @param tokenString token值
     * @param mContext    调用Activity
     */
    fun payAli(payUrl: String, orderNumber: String, tokenString: String, mContext: Activity) {
        val stringBuilder = StringBuilder()
        stringBuilder.append("orderNumber=")
                .append(orderNumber)
                .append("&signkey=")
                .append("ec3c9d73689c4e24ae7e4783dda56a60")
        val sign = MD5Util.md5(stringBuilder.toString())
        OkGo.post<String>(payUrl)
                .params("access_token", tokenString)
                .params("orderNumber", orderNumber)
                .params("sign", sign)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>) {
                        val type = object : TypeToken<ResponseEntity<AliInfo>>() {

                        }.type
                        val entity = JsonUtil.fromJson<ResponseEntity<AliInfo>>(response.body(), type)
                        if (null == entity || entity.data.zhiFuBaoResponse == null) {
                            Toast.makeText(mContext, "支付失败", Toast.LENGTH_SHORT).show()
                            return
                        }
                        val zhiFuBaoResponse = entity.data.zhiFuBaoResponse
                        AliPay.instance.pay(zhiFuBaoResponse!!, mContext)
                    }

                })
    }

    /**
     * 调用微信支付
     *
     * @param orderNumber
     * @param tokenString
     * @param context
     */
    fun payWeixin(url: String, orderNumber: String, tokenString: String, context: Activity) {
        val stringBuilder = StringBuilder()
        stringBuilder.append("orderNumber=")
                .append(orderNumber)
                .append("&signkey=")
                .append("ec3c9d73689c4e24ae7e4783dda56a60")
        val sign = MD5Util.md5(stringBuilder.toString())
        OkGo.post<String>(url)
                .params("access_token", tokenString)
                .params("orderNumber", orderNumber)
                .params("sign", sign)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>) {
                        val type = object : TypeToken<ResponseEntity<WeiXinInfo>>() {

                        }.type
                        val entity = JsonUtil.fromJson<ResponseEntity<WeiXinInfo>>(response.body(), type)
                                ?: return
                        val weiXinResponse = entity.data.weiXinResponse
                        val wXpay = WeiXinPay(weiXinResponse!!)
                        wXpay.createWXAPI(context)
                        wXpay.sendPayReq()

                    }

                })


    }
}
